import { request } from '@/utils/request';
import type { 
  User, 
  AuthUser, 
  DashboardStats, 
  MatchRecord, 
  InteractionRecord,
  PageParams,
  PageResult,
  QueryUserprofileRequest,
  QueryUserprofileResponse,
  SyncMatePreferenceRequest,
  SyncMatePreferenceResponse,
  SyncUserprofileRequest,
  SyncUserprofileResponse,
  QueryMatePreferenceRequest,
  QueryMatePreferenceResponse,
  UserAllInfoRequest,
  UserAllinfoResponse
} from '@/types';

// 认证相关API
export const authAPI = {
  // 登录
  login: (params: { account: string; password: string }) => {
    return request.post<{
      user: AuthUser;
      token: string;
    }>('/auth/login', params);
  },

  // 退出登录
  logout: () => {
    return request.post('/auth/logout');
  },

  // 获取当前用户信息
  getCurrentUser: () => {
    return request.get<AuthUser>('/auth/current');
  },
};

// 用户管理API
export const userAPI = {
  // 获取用户档案列表
  getUserProfileList: (params: QueryUserprofileRequest) => {
    return request.post<QueryUserprofileResponse>('/matchmaking/userprofile/query', params);
  },

  // 获取用户详情
  getUserDetail: (id: string) => {
    return request.get<User>(`/users/${id}`);
  },

  // 创建用户
  createUser: (data: Partial<User>) => {
    return request.post<User>('/users', data);
  },

  // 更新用户信息
  updateUser: (id: string, data: Partial<User>) => {
    return request.put<User>(`/users/${id}`, data);
  },

  // 删除用户
  deleteUser: (id: string) => {
    return request.delete(`/users/${id}`);
  },

  // 批量操作用户
  batchOperation: (userIds: string[], operation: string, data?: any) => {
    return request.post('/users/batch', { userIds, operation, data });
  },

  // 发送通知
  sendNotification: (userId: string, data: { type: string; content: string }) => {
    return request.post(`/users/${userId}/notification`, data);
  },

  // 同步择偶偏好
  syncMatePreference: (data: SyncMatePreferenceRequest) => {
    return request.post<SyncMatePreferenceResponse>('/matchmaking/matepreference/sync', data);
  },

  // 同步用户档案
  syncUserProfile: (data: SyncUserprofileRequest) => {
    return request.post<SyncUserprofileResponse>('/matchmaking/userprofile/sync', data);
  },

  // 查询择偶偏好
  queryMatePreference: (data: QueryMatePreferenceRequest) => {
    return request.post<QueryMatePreferenceResponse>('/matchmaking/matepreference/query', data);
  },

  // 获取用户全部信息
  getUserAllInfo: (data: UserAllInfoRequest) => {
    return request.post<UserAllinfoResponse>('/matchmaking/userprofile/allInfo', data);
  },
};

// 审核相关API
export const reviewAPI = {
  // 获取待审核列表
  getReviewList: (params: PageParams) => {
    return request.get('/reviews', params);
  },

  // 提交审核结果
  submitReview: (reviewId: string, data: { result: 'approved' | 'rejected'; notes: string }) => {
    return request.post(`/reviews/${reviewId}/submit`, data);
  },

  // 批量审核
  batchReview: (reviewIds: string[], result: 'approved' | 'rejected', notes: string) => {
    return request.post('/reviews/batch', { reviewIds, result, notes });
  },
};

// 匹配管理API
export const matchAPI = {
  // 获取待牵线列表
  getMatchList: (params: PageParams) => {
    return request.get<PageResult<MatchRecord>>('/matches', params);
  },

  // 创建匹配
  createMatch: (data: { userAId: string; userBId: string; notes?: string }) => {
    return request.post<MatchRecord>('/matches', data);
  },

  // 获取匹配记录
  getMatchRecords: (params: PageParams) => {
    return request.get<PageResult<MatchRecord>>('/matches/records', params);
  },

  // 获取互动记录
  getInteractionRecords: (params: PageParams) => {
    return request.get<PageResult<InteractionRecord>>('/interactions', params);
  },
};

// 搜索相关API
export const searchAPI = {
  // 高级搜索用户
  advancedSearch: (params: any) => {
    return request.post<PageResult<User>>('/search/users', params);
  },

  // 保存搜索条件
  saveSearchCondition: (data: { name: string; conditions: any }) => {
    return request.post('/search/conditions', data);
  },

  // 获取搜索历史
  getSearchHistory: () => {
    return request.get('/search/history');
  },
};

// 统计数据API
export const statsAPI = {
  // 获取仪表板统计数据
  getDashboardStats: () => {
    return request.get<DashboardStats>('/dashboard/stats');
  },

  // 获取用户增长数据
  getUserGrowthData: (params: { startDate: string; endDate: string }) => {
    return request.get('/stats/user-growth', params);
  },

  // 获取匹配成功率数据
  getMatchSuccessData: (params: { startDate: string; endDate: string }) => {
    return request.get('/stats/match-success', params);
  },

  // 获取用户分布数据
  getUserDistributionData: () => {
    return request.get('/stats/user-distribution');
  },

  // 导出数据
  exportData: (type: string, params: any) => {
    return request.post(`/export/${type}`, params);
  },
};

// 系统设置API
export const settingsAPI = {
  // 获取系统配置
  getSystemConfig: () => {
    return request.get('/settings/system');
  },

  // 更新系统配置
  updateSystemConfig: (data: any) => {
    return request.put('/settings/system', data);
  },

  // 获取操作日志
  getOperationLogs: (params: PageParams) => {
    return request.get('/logs/operations', params);
  },
};

// 文件上传API
export const uploadAPI = {
  // 上传单个文件
  uploadFile: (file: File, onProgress?: (percent: number) => void) => {
    return request.upload<{ url: string; filename: string }>('/upload/file', file, onProgress);
  },

  // 批量上传文件
  uploadFiles: (files: File[]) => {
    const formData = new FormData();
    files.forEach((file, index) => {
      formData.append(`files[${index}]`, file);
    });
    
    return request.post<Array<{ url: string; filename: string }>>('/upload/files', formData);
  },
};